package com.dao;

import java.sql.Timestamp;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.base.BaseDao;
import com.base.PageBean;
import com.base.UserBean;
import com.bean.DrawMoneyRequestRecord;
import com.bean.ParkManager;
import com.bean.ParkOrder;
import com.util.GUtil;

/**
 	* A data access object (DAO) providing persistence and search support for DrawMoneyRequestRecord entities.
 			* Transaction control of the save(), update() and delete() operations 
		can directly support Spring container-managed transactions or they can be augmented	to handle user-managed Spring transactions. 
		Each of these methods provides additional information for how to configure it for the desired type of transaction control. 	
	 * @see com.bean.DrawMoneyRequestRecord
  * @author MyEclipse Persistence Tools 
 */
public class DrawMoneyRequestRecordDao extends BaseDao {
	     private static final Logger log = Logger.getLogger(DrawMoneyRequestRecordDao.class);
		//property constants
	public static final String AMOUNT = "amount";
	public static final String ACCOUNT_BALANCE = "accountBalance";
	public static final String REAL_NAME = "realName";
	public static final String COMMENT = "comment";
	public static final String STATUS = "status";



    
    public void save(DrawMoneyRequestRecord transientInstance) {
        log.debug("saving DrawMoneyRequestRecord instance");
        try {
            getSession().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(DrawMoneyRequestRecord persistentInstance) {
        log.debug("deleting DrawMoneyRequestRecord instance");
        try {
            getSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public DrawMoneyRequestRecord findById( java.lang.Long id) {
        log.debug("getting DrawMoneyRequestRecord instance with id: " + id);
        try {
            DrawMoneyRequestRecord instance = (DrawMoneyRequestRecord) getSession()
                    .get("com.bean.DrawMoneyRequestRecord", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(DrawMoneyRequestRecord instance) {
        log.debug("finding DrawMoneyRequestRecord instance by example");
        try {
            List results = getSession()
                    .createCriteria("com.bean.DrawMoneyRequestRecord")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding DrawMoneyRequestRecord instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from DrawMoneyRequestRecord as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByAmount(Object amount
	) {
		return findByProperty(AMOUNT, amount
		);
	}
	
	public List findByAccountBalance(Object accountBalance
	) {
		return findByProperty(ACCOUNT_BALANCE, accountBalance
		);
	}
	
	public List findByRealName(Object realName
	) {
		return findByProperty(REAL_NAME, realName
		);
	}
	
	public List findByComment(Object comment
	) {
		return findByProperty(COMMENT, comment
		);
	}
	
	public List findByStatus(Object status
	) {
		return findByProperty(STATUS, status
		);
	}
	

	public List findAll() {
		log.debug("finding all DrawMoneyRequestRecord instances");
		try {
			String queryString = "from DrawMoneyRequestRecord";
	         Query queryObject = getSession().createQuery(queryString);
			 return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
    public DrawMoneyRequestRecord merge(DrawMoneyRequestRecord detachedInstance) {
        log.debug("merging DrawMoneyRequestRecord instance");
        try {
            DrawMoneyRequestRecord result = (DrawMoneyRequestRecord) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(DrawMoneyRequestRecord instance) {
        log.debug("attaching dirty DrawMoneyRequestRecord instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(DrawMoneyRequestRecord instance) {
        log.debug("attaching clean DrawMoneyRequestRecord instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
	// 收入提取的query，-1：未付款；1：未处理；2：处理中；3：已付款
	public List<DrawMoneyRequestRecord> querydrawmoney(DrawMoneyRequestRecord object, UserBean userBean,PageBean page) {
		session = getSession();
		log.debug("querydrawmoney ParkOrder instances");
		StringBuilder sb=new StringBuilder();
		try {
			ParkManager pm = (ParkManager) session.get(ParkManager.class, userBean.getId());
			if (pm == null)
				return null;
			sb.append("from DrawMoneyRequestRecord where 1=1 ");
			if(object!=null){
				if(object.getAccountBalance()!=null&&!"".equals(object.getAccountBalance())){
					sb.append(" and accountBalance  like '%" +object.getAccountBalance()+ "%')");
				}
				if(object.getParkManager().getRealName()!=null&&!"".equals(object.getParkManager().getRealName())){
					sb.append(" and parkManager.realName like '%" +object.getParkManager().getRealName()+ "%'");
				}
				if(object.getParkManager().getUserName()!=null&&!"".equals(object.getParkManager().getUserName())){
					sb.append(" and parkManager.userName like '%" +object.getParkManager().getUserName()+ "%'");
				}
			}
			if( object!=null && object.getStatus()!=null ){
				sb.append(" and status=" +object.getStatus()+ "");
			}else{
				sb.append(" and status in(1,2)");
			}
			sb.append(" order by id,status");
			Long count = (Long) session.createQuery("select count(*) "+ sb.toString()).uniqueResult();
			if(page!=null && count!=null )
				page.setTotalRow(count.intValue());
				Query queryObject = session.createQuery(sb.toString());
				//queryObject.setLong(0,pm.getUserId());
			if(page!=null && page.getPageSize()>0 && page.getCurrentPage()>0){
					queryObject.setFirstResult( (page.getCurrentPage()-1)*page.getPageSize());
					queryObject.setMaxResults( page.getPageSize() );
				}
				
			return queryObject.list();
		} catch (RuntimeException e) {
			log.error("attach failed", e);
            throw e;
		}
	}
	
	public String update(DrawMoneyRequestRecord record) {
		log.debug("merging DrawMoneyRequestRecord instance");
		session=getSession();
		try {
			session.update(record);
			session.flush();
			log.debug("merge successful");
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
		return GUtil.SUCCESS;
	}
	public DrawMoneyRequestRecord get(Long id){
		log.debug("getting DrawMoneyRequestRecord instance with id: " + id);
		try {
			DrawMoneyRequestRecord instance = (DrawMoneyRequestRecord) getSession().get(DrawMoneyRequestRecord.class, id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed id="+id, re);
			throw re;
		}
	}
}